﻿//-----------------------------------------------------------------------
// <copyright file="SetParentEntityAction.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Action
{
    using System;
    using System.Collections.Generic;
    using TileTool.Log;
    using TileTool.Scene.Entity;

    /// <summary>
    /// Action to set the parent of entities.
    /// </summary>
    public class SetParentEntityAction : SceneAction
    {
        #region Constants - Private

        /// <summary>
        /// Log message group.
        /// </summary>
        private const string LogGroup = "SetParentEntityAction";

        #endregion

        #region Fields - Private

        /// <summary>
        /// List of entities we will modify.
        /// </summary>
        private IList<ParentData> entityData = new List<ParentData>();

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor for the action.
        /// </summary>
        /// <param name="manager">Scene this action operates on.</param>
        public SetParentEntityAction(ISceneManager manager)
            : base(manager)
        {
            this.Valid = false;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Add an entity to have its parent modified.
        /// </summary>
        /// <param name="entity">Entity to modify.</param>
        /// <param name="newParent">New parent entity.</param>
        public void AddEntity(IEntity entity, IEntity newParent)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity", "Entity must be non-null.");
            }

            ParentData data = new ParentData(entity, entity.Parent, newParent);

            this.entityData.Add(data);
            this.Valid = true;
        }

        /// <summary>
        /// Do the action.
        /// </summary>
        public override void Do()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Do() - Setting parent for " + this.entityData.Count + " entities.");

            foreach (ParentData data in this.entityData)
            {
                data.Entity.Parent = data.NewParent;
            }
        }

        /// <summary>
        /// Undo the action.
        /// </summary>
        public override void Undo()
        {
            if (!this.Valid)
            {
                return;
            }

            LogSystem.LogInfo(LogGroup, "Undo() - Setting parent for " + this.entityData.Count + " entities.");

            foreach (ParentData data in this.entityData)
            {
                data.Entity.Parent = data.OriginalParent;
            }
        }

        #endregion

        #region Class - Private

        /// <summary>
        /// Class which wraps parent data we cache off.
        /// </summary>
        private class ParentData
        {
            /// <summary>
            /// Constructor for the data class.
            /// </summary>
            /// <param name="entity">Entity we are operating on.</param>
            /// <param name="originalParent">Original parent.</param>
            /// <param name="newParent">New parent.</param>
            public ParentData(IEntity entity, IEntity originalParent, IEntity newParent)
            {
                this.Entity = entity;
                this.OriginalParent = originalParent;
                this.NewParent = newParent;
            }

            /// <summary>
            /// Gets the entity we are operating on.
            /// </summary>
            public IEntity Entity
            {
                get;
                private set;
            }

            /// <summary>
            /// Gets the original entity parent.
            /// </summary>
            public IEntity OriginalParent
            {
                get;
                private set;
            }

            /// <summary>
            /// Gets the new entity parent.
            /// </summary>
            public IEntity NewParent
            {
                get;
                private set;
            }
        }

        #endregion
    }
}
